local zhihuan = fk.CreateSkill{
    name = "rmt__zhihuan",
}
Fk:loadTranslationTable{
    ["rmt__zhihuan"] = "置换",
    [":rmt__zhihuan"] = "其他角色的回合结束时，若你和其本回合都使用过牌，你可以对其造成1点火焰伤害，然后用一个技能交换其一个技能。",
    ["#rmt__zhihuan-invoke"] = "是否发动 置换，对 %dest 造成1点火焰伤害，并与其交换一个技能",
    ["#rmt__zhihuan-prompt"] = "置换：请选择要用 %arg 交换 %dest 的一个技能",
}

zhihuan:addEffect(fk.TurnEnd,{
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(zhihuan.name) and target ~= player then
            local room = player.room
            local skills = {}
            for _, skill in ipairs(target.player_skills) do
                if skill:isPlayerSkill(target) then
                    table.insert(skills, skill.name)
                end
            end
            return #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
                local use = e.data
                return use.from == player
            end, Player.HistoryTurn) > 0 and #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
                local use = e.data
                return use.from == target
            end, Player.HistoryTurn) > 0 and #skills > 0
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local skills = {}
        for _, skill in ipairs(player.player_skills) do
            if skill:isPlayerSkill(player) then
                table.insert(skills, skill.name)
            end
        end
        if #skills > 0 then
            table.insert(skills,"Cancel")
            local choice = room:askToChoice(player,{
                choices = skills,
                skill_name = zhihuan.name,
                prompt = "#rmt__zhihuan-invoke::"..target.id,
                detailed = true,
            })
            if choice ~= "Cancel" then
                event:setCostData(self, choice)
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local skills = {}
        local cost_data = event:getCostData(self)
        room:damage { from = player, to = target, damage = 1, skillName = zhihuan.name, damageType = fk.FireDamage }
        for _, skill in ipairs(target.player_skills) do
            if skill:isPlayerSkill(target) then
                table.insert(skills, skill.name)
            end
        end
        if #skills > 0 and not target.dead then
            local choice = room:askToChoice(player,{
                choices = skills,
                skill_name = zhihuan.name,
                prompt = "#rmt__zhihuan-prompt::"..target.id,
                detailed = true,
            })
            room:handleAddLoseSkills(player,"-"..cost_data,nil)
            room:handleAddLoseSkills(target,cost_data,nil)
            room:handleAddLoseSkills(target,"-"..choice,nil)
            room:handleAddLoseSkills(player,choice,nil)
        end
    end,
})

return zhihuan